package com.xk.demo.user.control;


import com.xk.demo.common.control.BaseController;
import com.xk.demo.mybatis.entity.PtUserEntity;
import com.xk.demo.mybatis.flexigrid.PadingRstType;
import com.xk.demo.mybatis.flexigrid.PagingBean;
import com.xk.demo.mybatis.ztree.ZtreeEntity;
import com.xk.demo.user.entity.UserSearchEntity;
import com.xk.demo.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private Logger logger =  LogManager.getLogger(this.getClass().getName());
    //业务逻辑层
    @Resource
    private UserService userService;
    @Resource
    private MessageSource messageSource;
    @Secured("ROLE_gqgy_data_user")
//    @Secured("EOLE_ADMIN")

    @RequestMapping("/userPage")
    public String userPage(){
        return "/user/userPage";
    }
    @RequestMapping("/getUserInfolistByPage")//分页
    @ResponseBody
    public PadingRstType<PtUserEntity> getUserInfolistByPage(UserSearchEntity search, PagingBean paging){
        logger.info(search);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.getUserInfolistByPage(search,paging);
        return ptUserEntityPadingRstType;
    }
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public String deleteUserById(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid"+userUuid);
        userService.deleteUserById(userUuid);
        return getSuccessMessag("user.delete.sucess");

    }

    @RequestMapping("/getRoleZtreeListByUserId")
    @ResponseBody
    public List<ZtreeEntity> getRoleZtreeListByUserId(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid"+userUuid);
        List<ZtreeEntity> list=userService.getRoleZtreeListByUserId(userUuid);
        return list;

    }
    @RequestMapping("/saveRoleByUserId")
    @ResponseBody
    public String saveRoleByUserId(@RequestParam("userUuid") String userUuid,@RequestParam("roleUuids") String roleUuids){
        logger.info("userUuid:"+userUuid+" roleUuids:"+roleUuids);
        userService.saveRoleByUserId(userUuid,roleUuids);
        return getSuccessMessag("user.allocat.role.success");

    }
    @RequestMapping("/addUser")
    @ResponseBody
    public void addUser(PtUserEntity user){
        userService.addUser(user);
    }

    @RequestMapping("/updateUserByUserId")
    @ResponseBody
    public void updateUserByUserId(PtUserEntity user){
        System.out.println(user.getUserName());
        System.out.println(user.getUserUuid());
        userService.updateUserByUserId(user);
    }
}
